package com.andeveloper.springdemo.service.impl.welcome;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.andeveloper.springdemo.dao.UserDao;
import com.andeveloper.springdemo.model.User;
import com.andeveloper.springdemo.service.welcome.WelcomeService;

@Service("welcomeService")
public class WelcomeServiceImpl implements WelcomeService {

	@Resource
	private UserDao userDao;
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT, rollbackFor=Exception.class, timeout=30)
	public int addUpVisitTimes(String userName) {
		int times = 0;
		if(StringUtils.isBlank(userName)) {
			return times;
		}
		User user = userDao.findUserByName(userName);
		if(user == null) {
			user = new User();
			user.setUserName(userName);
			times = 1;
			user.setVisitTimes(times);
			userDao.saveUser(user);
			return times;
		}
		times = user.getVisitTimes() + 1;
		userDao.addUpVisitTimes(user.getUserId());
		return times;
	}
	
}
